package com.ddd.mill.infrastructure.advice;


import com.ddd.mill.infrastructure.annotation.AccountPattern;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author liu
 * @title: AccountPatternDataADvice
 * @projectName user-enterprise-center
 * @date 2020/11/2312:01
 */
public class AccountPatternDataAdvice implements ConstraintValidator<AccountPattern,String> {

    private static Pattern EmailPattern = Pattern.compile("^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\\.[a-zA-Z0-9_-]+)+$");

    private static Pattern MobilePattern=Pattern.compile("^((13[0-9])|(14[5,7])|(15[^4,\\\\D])|(17[0,1,3,6-8])|(18[0-9])|(19[8,9])|(166))[0-9]{8}$");

    @Override
    public void initialize(AccountPattern constraintAnnotation) {

    }

    @Override
    public boolean isValid(String s, ConstraintValidatorContext constraintValidatorContext) {
        Matcher matcher = EmailPattern.matcher(s);
        if (!matcher.matches()){
            matcher=MobilePattern.matcher(s);
            return matcher.matches();
        }else {
            return true;
        }
    }
}
